<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <!-- logo区域 -->
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <!-- 表单区域 -->

      <el-form ref="form" :model="form" class="login-form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="form.isAgree" label="我已阅读并同意用户协议和隐私条款"></el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click.prevent="hLogin">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入合法手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '请输入合法的验证码', trigger: 'blur' }
        ]
      },
      form: {
        isAgree: false,
        mobile: '13911111111', // 手机号
        code: '246810' // 密码
      }
    }
  },
  methods: {
    doLogin () {
      const { mobile, code } = this.form
      // eslint-disable-next-line no-undef
      axios({
        method: 'POST',
        url: 'http://api-toutiao-web.itheima.net/mp/v1_0/authorizations',
        data: {
          // eslint-disable-next-line no-undef
          mobile,
          // eslint-disable-next-line no-undef
          code
        }
      }).then(res => {
        console.log('登陆成功')
        // 成功提示消息
        this.$message({
          message: '恭喜你，登录成功',
          type: 'success'
        })
      }).catch(err => {
        console.log(err)
        // 警告提示消息
        this.$message({
          message: '警告哦，这是一条警告消息',
          type: 'warning'
        })
      })
    },
    hLogin () {
      this.$refs.form.validate(valid => {
        // 验证通过
        if (valid) {
          this.doLogin()
        }
      })
    }
  }
}
</script>
